<?php
//=========
$bduss="";//请填写bduss值
$kw="chrome";//请填写吧名
$del_num=5;//每检测多少贴后删除
$max_thread_types=10000000;//设置检测参数最大值
//=========
$do_del_id=array();
$fid;
$tbs;

function curl_client($pda,$url="http://c.tieba.baidu.com/c/f/pb/page"){
	global $bduss;
	$header = array ("Content-Type: application/x-www-form-urlencoded");
	$pda=array_merge(array("BDUSS=".$bduss,
		"_client_id=04-00-DA-69-15-00-73-97-08-00-02-00-06-00-3C-43-01-00-34-F4-22-00-BC-35-19-01-5E-46",
		"_client_type=2",
		"_client_version=5.6.3",
		"_phone_imei=642b43b58d21b7a5814e1fd41b08e2a6",
		"from=tieba"),$pda);
	$data=implode("&", $pda)."&sign=".md5(implode("", $pda)."tiebaclient!!!");
	//echo $data.'<br>';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$re = json_decode(curl_exec($ch)); 
	curl_close($ch);
	return $re;
}
function curl_post($pda,$url){
    global $bduss;
	$cookie="BDUSS=$bduss";
	$data=implode("&", $pda);
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_COOKIE,$cookie); 
    $re = json_decode(curl_exec($ch)); 
    curl_close($ch);
    return $re;
}
function get_list(){
	global $kw,$max_thread_types,$fid,$tbs,$do_del_id,$del_num;
	$data=array("kw=".$kw,
            "pn=1",
            "q_type=2",
            "rn=50",
            "with_group=1");
	//print_r(curl_client($data,"http://c.tieba.baidu.com/c/f/frs/page"));
	$re=curl_client($data,"http://c.tieba.baidu.com/c/f/frs/page");
	//print_r($re);
	$tbs=$re->anti->tbs;
	$fid=$re->forum->id;
	$re=$re->thread_list;
	echo "已找到要处理的贴子:<br>";
	foreach ($re as $key => $value) {
		if($value->thread_types>$max_thread_types){//&&count($value->media)==0
			echo $value->title." | ";
			//echo $value->id."<br>";
			if($value->is_activity==2){
				$do_del_id[count($do_del_id)]=$value->id;
				echo "活动转贴，已积累[".count($do_del_id)."]<br>";
			}else if($value->thread_type==0&&count($value->media)==0){
				test17($value->id);
			}else echo "非知道贴/活动转贴<br>";

			if(count($do_del_id)>=$del_num)dodel();
		}
	}
	dodel();
}
function test17($id){
	global $do_del_id;
	$data=array("kz=".$id,
            "pn=1",
            "q_type=2",
            "rn=2",
            "with_floor=1");
	$re=curl_client($data,"http://c.tieba.baidu.com/c/f/pb/page");
	if($re->thread->thread_type==17){
		$do_del_id[count($do_del_id)]=$id;
		echo "知道贴，已积累[".count($do_del_id)."]<br>";
	}
	/*else if($re->thread->thread_type==8&&$re->thread->is_activity==2){
		$do_del_id[count($do_del_id)]=$id;
		echo "活动转贴，已积累[".count($do_del_id)."]<br>";
	}*/
	else echo "非知道贴/活动转贴<br>";
}
function dodel(){
	global $do_del_id,$fid,$kw,$tbs;
	if(count($do_del_id)==0){
		echo "没有要删除的知道贴/活动转贴！！！<br>";
		return;
	}
	$data=array("fid=".$fid,
			"ie=utf-8",
			"isBan=0",
			"kw=".$kw,
			"tbs=".$tbs,
			"tid=".implode("_", $do_del_id));
	$re=curl_post($data,"http://tieba.baidu.com/f/commit/thread/batchDelete");
	if($re->no==0)echo "删除知道贴[".count($do_del_id)."]<br>";
	$do_del_id=array();
}
get_list();
?>